#!/usr/bin/perl
use strict;
use warnings;
my $copyright = <<'EOF';
/* Copyright (C) 2007,2012,2013,2015 Olly Betts
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */
EOF

use Tokeniseise;

my $srcdir = shift @ARGV;

my $all_header = 'languages/allsnowballheaders.h';
open ALL_HEADER, '>', "$all_header~" or die $!;
my $all_guard = 'XAPIAN_INCLUDED_ALLSNOWBALLHEADERS_H';
print ALL_HEADER <<"EOF";
/** \@file $all_header
 *  \@brief Include headers for all Snowball stemmers
 */
/* Warning: This file is generated by $0 - do not modify directly! */
$copyright
#ifndef $all_guard
#define $all_guard

EOF

my $hdr = Tokeniseise->new('languages/sbl-dispatch.h', 'Map string to language code', $copyright, 'XAPIAN_INCLUDED_SBL_DISPATCH_H', 'sbl_code', 2);
$hdr->add('none', 'NONE');
my @langs;
foreach (sort @ARGV) {
    m{.*/(.*)\.sbl$} or die "Failed to parse snowball source: $_\n";
    my $lang = $1;
    push @langs, $lang;
    # Include the headers for all the snowball stemmers.
    print ALL_HEADER "#include \"$lang.h\"\n";
    my $enum = uc $lang;
    $hdr->add($lang, $enum);
    open SBL, '<', "$srcdir/$_" or die "Open $srcdir/$_ failed: $!\n";
    my $l = <SBL>;
    close SBL;
    $l =~ s/^\W*Alias:\s*//i or die "No Alias: header in $srcdir/$_\n";
    $l =~ s/\s*$//;
    for (split /\s+/, $l) {
	$hdr->add($_, $enum);
    }
}

print ALL_HEADER <<"EOF";

#endif
EOF
close ALL_HEADER or die $!;
rename "$all_header~", $all_header or die $!;

$hdr->append("#define LANGSTRING \"" . join(" ", @langs) . "\"");

$hdr->write();
